import sys
import string
import types
import os.path
import urllib
from datetime import datetime
import time
from xml.sax import saxexts
from xml.sax import saxlib
from UserStack import UserStack
import dateutilparser
import dateutilrelativedelta
import dateutilrrule
import commands
import os
from dateutilparser import parse

def get_attr_map(attrs):
	attrMap = {}
	for i in range(0, len(attrs)):
		if not attrMap.has_key(attrs.getName(i)):
			attrMap[attrs.getName(i)] = attrs.getValue(i)
	return attrMap

class VerySimpleHandler(saxlib.DocumentHandler):
	def __init__(self):
		self.appt_list = []
		self.path = " "
		self.myWeather = weatherInfo()
		self.tempForecast = "Your weather forecast "

	def startElement(self, name, attrs):
		self.path = os.path.join(self.path,name)
		attrMap = get_attr_map(attrs)
		if self.path.strip() == "/rss/channel/yweather:location":
			#Adding location
			self.myWeather.addLocation(attrMap["city"],attrMap["region"],attrMap["country"])
		elif self.path.strip() == "/rss/channel/yweather:astronomy":
			#Adding Sunrise and Sunset Information
			self.myWeather.addSun(attrMap["sunrise"],attrMap["sunset"])
		elif self.path.strip() == "/rss/channel/item/yweather:condition":
			temp_cond = "Current Conditions " + attrMap["text"] + " " + attrMap["temp"] + " Degrees " 
			self.myWeather.addConditions(temp_cond)
		elif self.path.strip() == "/rss/channel/item/yweather:forecast":
			self.tempForecast = self.tempForecast + " " + attrMap["day"] + " " + attrMap["date"] + " Low of " + attrMap["low"] + " High of " + attrMap["high"] + " " + attrMap["text"]
			self.myWeather.addForecast(self.tempForecast)
			self.tempForecast = "For "

	def endElement(self, name):
		(self.path,dont_care) = os.path.split(self.path)
		if self.path.strip() == "/rss":
			print "Printing completed information"
			print self.myWeather.printLocation()
			print self.myWeather.printSun()
			print self.myWeather.printTitle()
			print self.myWeather.printConditions()
			print self.myWeather.printForecast()
	
   	def characters(self, ch, start, length):
		#Printing char information for title
		if self.path.strip() == "/rss/channel/item/title":
			temp_info = ch[start:start+length]
			self.myWeather.addTitle(temp_info)
				
	def returnWeather(self):
		return self.weather

class weatherInfo:

	def __init__(self):
		self.city = ""
		self.region = ""
		self.country = ""
		self.sunrise = ""
		self.sunset = ""
		self.title = ""
		self.condition = ""
		self.forecast = []

	def addLocation(self,city,region,country):
		self.city = city
		self.region = region
		self.country = country

	def printLocation(self):
		print self.city + ", " + self.region + " " + self.country

	def addSun(self,sunrise,sunset):
		self.sunrise = sunrise
		self.sunset = sunset

	def printSun(self):
		print "Sunrise at " + self.sunrise + " Sunset at " + self.sunset
		
	def addTitle(self,title):
		self.title = title

	def printTitle(self):
		print self.title

	def addConditions(self,cond):
		self.condition = cond

	def printConditions(self):
		print self.condition

	def addForecast(self,info):
		self.forecast.append(info)

	def printForecast(self):
		for i in range(0,len(self.forecast)):
			print self.forecast[i]
	

#Method to create vxml files once text has been created
def strToFile(text,filename):
	output = file(filename,"w")
	output.write(text)
	output.close()



#Loading the data from Google
def loadData(zipcode):
	vsh = VerySimpleHandler()
	parser = saxexts.make_parser()
	parser.setDocumentHandler(vsh)
	weburl = "http://weather.yahooapis.com/forecastrss?p=" + str(zipcode)
	f = urllib.urlopen(weburl)
	infile = f.readlines()
	tempFile = ""
	newFile = ""
	i = 0
	while(i < len(infile)):
		newList = infile[i].split('\n')
		for item in newList:
			tempFile = tempFile + item
		i = i + 1

	tempFile = tempFile.split('&amp;nbsp')
	for item in tempFile:
		newFile = newFile + item
	tempFile = newFile.split('&lt;br&gt;')
	newFile = ''
	for item in tempFile:
		newFile = newFile + item

	#Opening the temp file to write the new xml info to
	myFile = open('tempFile.xml','w')
	myFile.write(newFile)
	myFile.close()
	#Opening the file to give to the parser
	myFile = open('tempFile.xml','r')
	parser.parseFile(myFile)



loadData(60423)
